import axios from 'axios'
import qs from 'qs'
import { ElMessage } from 'element-plus'

const instance = axios.create({
  baseURL: 'http://localhost:9000/boss',
  timeout: 50000
})

function doGet(path, parameter) {
  return instance({
    url: path,
    method: 'GET',
    params: parameter
  })
}

function doPost(path, parameter) {
  return instance({
    url: path,
    method: 'POST',
    data: qs.stringify(parameter)
  })
}

function doJson(path, parameter) {
  return instance({
    url: path,
    method: 'POST',
    data: parameter
  })
}

//应答拦截器
instance.interceptors.response.use(
  (server) => {
    if (server) {
      if (server.data.code === 1000) {
        return server.data
      } else {
        ElMessage.warning({
          showClose: true,
          message: server.data.msg
        })
        return Promise.resolve()
      }
    }
  },
  (error) => {
    console.log('axios应答拦截器错误：' + error)
    ElMessage.warning({
      showClose: true,
      message: error
    })
    return Promise.resolve()
  }
)

export { doGet, doPost, doJson }